1783B - Matrix of Differences - CodeForces Solution


constructive algorithms math

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
   n=int(input())
   q=n
   n=n*n
   l=list(range(1,(n//2)+1))
   s=list(range((n//2)+1,n+1))
   s=s[::-1]
   f=[]
   for i in range(n//2):
     f.append(l[i])
     f.append(s[i])
   if n%2!=0:
     f.append(s[-1])
   c=0
   for k in range(0,n,q):
     c+=1
     if c%2==0:
       print(' '.join(map(str,(f[k:k+q]))))
     else:
       print(' '.join(map(str,(f[k:k+q])[::-1])))
     
     

C++ Code:

#include <bits/stdc++.h>
#define ll long long int
#define lld long double
#define ull unsigned long long int
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
#include <iostream> 
using namespace __gnu_pbds; 
using namespace std; 
ll mod=998244353;
// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag, 
            tree_order_statistics_node_update> 
    ordered_set;
// Declaring ordered_multiset
typedef tree<int, null_type, 
             less_equal<int>, rb_tree_tag, 
             tree_order_statistics_node_update> 
    ordered_mset; 
ll NcR(ll n, ll r) {ll p = 1, k = 1; if (n - r < r)r = n - r; if (r != 0) {while (r) {p *= n; k *= r; ll m = __gcd(p, k); p /= m; k /= m; n--; r--;}} else p = 1; return p;}
ll modaddition(ll a, ll b, ll mod ) {return (a % mod + b % mod) % mod;}
using  namespace std;
void printv(vector<ll>&v)
{
    for(auto a:v)
        cout<<a<<" ";
    cout<<endl;
}
void inputv(vector<ll>&v)
{
    for(ll i=0;i<v.size();i++)
    {
        cin>>v[i];
    }
}
string decToBinary(ll n)
{
    // Size of an integer is assumed to be 32 bits
    string s="";
    for (int i = 31; i >= 0; i--) {
        int k = n >> i;
        if (k & 1)
            s+='1';
        else
            s+='0';
    }
    return s;
}
inline ll ceil(ll a , ll b){ if(a%b==0) return a/b ; else return a/b+1 ; } 
void fun(ll tt)
{
    ll n;
    cin>>n;
    ll l,r;
    l=1;
    r=n*n;
    vector<vector<ll>>v(n,vector<ll>(n,0));
    ll i=0,j=0;
    while(i<n)
    {
        v[i][j]=l;
        l++;
        i++;
        j++;
    }
    ll il1=2,jl1=0,il2=0,jl2=2,ir1=1,jr1=0,ir2=0,jr2=1;
    bool flip=false;
    while(true)
    {
        // cout<<l<<" "<<r<<endl;
        if(l>r)
        {
            // cout<<"Break\n";
            break;
        }
        
        if(!flip)
        {
            ll i=ir1,j=jr1;
            while(i<n && j<n)
            {
                v[i][j]=r;
                r--;
                i++;
                j++;
            }
            i=ir2,j=jr2;
            while(i<n && j<n)
            {
                v[i][j]=r;
                r--;
                i++;
                j++;
            }
            ir1+=2;
            jr2+=2;
        }
        else
        {
            ll i=il1,j=jl1;
            while(i<n && j<n)
            {
                v[i][j]=l;
                l++;
                i++;
                j++;
            }
            i=il2,j=jl2;
            while(i<n && j<n)
            {
                v[i][j]=l;
                l++;
                i++;
                j++;
            }
            il1+=2;
            jl2+=2;
        }
        flip=!flip;
    }
    for(auto x:v)
        printv(x);
    
}  

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    // t=1;
    for (ll i = 1; i <= t; i++)
    {
        fun(t);
    }
}


Comments

Submit
0 Comments
More Questions

1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal